package com.yanqu.road.server.cmd.activity.holiday;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.holiday.OperateCatsResult;
import com.yanqu.road.entity.activity.holiday.UserHolidayActivityData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.holidayactivity.HolidayActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.holiday.HolidayActivityModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.HolidayActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.pb.HolidayActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = Protocol.S_HOLIDAY_ACTIVITY_RECRUIT_CATS, desc = "招募")
public class RecruitCatsCmd extends AbstractCommand {

    private int activityId;

    private int recruitType;

    OperateCatsResult result;

    ActivityInfo activityInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HolidayActivityProto.RecruitCatsReqMsg reqMsg = HolidayActivityProto.RecruitCatsReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        recruitType = reqMsg.getRecruitType();
        activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(!SystemOpenMgr.systemOpen(player, eSystemId.CatTreasure.getValue())) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!ActivityMgr.activityInTime(activityInfo)){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        Property zhaoMuComsume = new Property();
        List<Integer> param4List = HolidayActivityMgr.getActivityParam4(activityInfo);
        int recruitTimes;
        if(1 == recruitType){
            recruitTimes = 1;
            zhaoMuComsume.setGood(param4List.get(0), BigInteger.ONE);
        }else {
            recruitTimes = 10;
            zhaoMuComsume.setGood(param4List.get(0), BigInteger.valueOf(9L));   //消耗9个招募令
        }
        //招募令是否充足
        if(!player.getModule(CurrencyModule.class).removeCurrency(zhaoMuComsume, eLogMoneyType.Activity, eLogMoneyType.HolidayActivityUseGoods)){
            ret = GameErrorCode.E_HOLIDAY_ACTIVITY_GOODS_NO_ENOUGH;
            return;
        }
        result = player.getModule(HolidayActivityModule.class).recruitCats(activityInfo, recruitTimes);
        ret = result.getRet();
    }

    @Override
    public void sendData(GamePlayer player) {
        HolidayActivityProto.RecruitCatsRespMsg.Builder respMsg = HolidayActivityProto.RecruitCatsRespMsg.newBuilder();
        if(ret == 0){
            respMsg.setReward(PropertyHelper.parsePropertyToString(result.getReward()));
            UserHolidayActivityData userData = player.getModule(HolidayActivityModule.class).getUserHolidayActivityDataByActivityId(activityId);
            HolidayActivityProto.UserCatsMergeDataTemp.Builder userDataTempBuilder = HolidayActivityPb.parseUserCatsMergeDataTempBuilder(HolidayActivityMgr.getCatsInfoByActivityId(activityId), userData);
            respMsg.setUserCatsData(userDataTempBuilder);
        }
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_HOLIDAY_ACTIVITY_RECRUIT_CATS, respMsg);
    }
}
